package cn.stylefeng.guns.modular.cb.service;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.modular.cb.mapper.CbCustomerCompanyMapper;
import cn.stylefeng.guns.modular.cb.mapper.CbCustomerStaffMapper;
import cn.stylefeng.guns.modular.cb.mapper.CbCustomerStudentsMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerCompanyParam;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerStaffParam;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerStudentsParam;
import cn.stylefeng.guns.modular.cb.model.params.CbWxUserParam;
import cn.stylefeng.guns.modular.cb.util.SMSUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.mapper.DeptMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.PositionMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.RoleMapper;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
public class CustomerLoginService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private DeptMapper deptMapper;


    @Autowired
    private CbCustomerCompanyMapper companyMapper;

    @Autowired
    private CbCustomerStaffMapper staffMapper;

    @Autowired
    private CbCustomerStudentsMapper studentsMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CbWxUserService cbWxUserService;

    @Autowired
    private CbCustomerStudentsService studentsService;

    @Autowired
    private CbCustomerCompanyService companyService;

    @Autowired
    private CbCustomerStaffService staffService;


    @Value("${wx.appid}")
    private String appid;
    @Value("${wx.secret}")
    private String secret;
    @Value("${login.pwd}")
    private String pwd;

    /**
     * 添加用戶
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:51
     */
    @Transactional(rollbackFor = Exception.class)
    public void addUser(UserDto user) {
        String name = "客户";
        //1.添加用户部门
        user.setDeptId(26L);

        //2.用户职位信息
        //空

        //3.添加角色
//        user.setRoleId(roleId);

        //5.添加用户手机号
        user.setPhone(user.getAccount());
        //6.添加账号
        userService.addUser(user);
    }

    /**
     * 设置用户账号
     * @param userId
     * @param account
     * @return
     */
    public int setAccount(Long userId, String account){
        return userService.setAccount(userId, account);
    }

    /**
     * 微信openid获取用户
     * @param openId
     * @return
     */
    public User getUserByOpenId(String openId){
        return userService.getByOpenId(openId);
    }

    /**
     * 删除用户
     */
    public void deleteUser(){
        LoginUser user = LoginContextHolder.getContext().getUser();
        userService.deleteUser(user.getId());
    }

    /**
     * 获取用户openId
     * @param code
     * @return
     */
    public JSONObject saveWxOpenId(String code){
//        String appid = "wx29cacb46d1cd1e76";
//        String secret = "0340c0c62356dd0691c4e1b195766513";
        String url =    "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid +
                "&secret="  + secret +
                "&js_code=" + code   +
                "&grant_type=authorization_code";
        String jsonData = this.restTemplate.getForObject(url,String.class);
        JSONObject data = JSON.parseObject(jsonData);
        String openid = data.getString("openid");
        String sessionKey = data.getString("session_key");

        //保存或更新sessionkey
        CbWxUserParam param = new CbWxUserParam();
        param.setOpenid(openid);
        param.setSessionKey(sessionKey);
        cbWxUserService.saveWx(param);

        return data;
    }

    /**
     * 设置手机号
     * @param id
     * @param phone
     */
    public void setPhone(Long id, String phone){
        LoginUser user = LoginContextHolder.getContext().getUser();

        //设置用户手机号
        List<Long> roleList = user.getRoleList();
        Long roleId = roleList.get(0);
        if (roleId == 6){//企业
            CbCustomerCompanyParam param = new CbCustomerCompanyParam();
            param.setId(id);
            param.setPhone(phone);
            companyService.update(param);
        }else if (roleId == 7){//职员
            CbCustomerStaffParam param = new CbCustomerStaffParam();
            param.setId(id);
            param.setPhone(phone);
            staffService.update(param);
        }else if (roleId == 8){//学生
            CbCustomerStudentsParam param = new CbCustomerStudentsParam();
            param.setId(id);
            param.setPhone(phone);
            studentsService.update(param);
        }

        //设置账号
        this.setAccount(user.getId(), phone);
    }
}
